package cn.edu.wzu.openlearning.service;


import cn.edu.wzu.openlearning.mapper.UserMapper;
import cn.edu.wzu.openlearning.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public User loginOrRegister(User user){
        User realUser = userMapper.selectUserByUsername(user.getUsername());
        if(realUser == null) {
            // 代表没有用户,注册用户
            userMapper.registerUser(user);
            realUser = userMapper.selectUserByUsername(user.getUsername());
            // TODO 更新用户的最后登录时间

            return realUser;
        } else {
            // 用户存在，匹配密码，如果密码不正确，则返回None
            if (user.getPassword().equalsIgnoreCase(realUser.getPassword())) {
                return realUser;
            } else {
                return null;
            }
        }
    }

}
